<template>
  <div class="comment" :id="`comment${comment.id}`">
    <div class="comment-head">
      <div class="user-avatar"><img :src="comment.fromUserAvatar" alt=""></div>
      <div class="head-right">
        <section-title>
          <div style="display: flex;align-items: center;justify-content: space-between;">
            <div>
              <span class="from-user user-name">{{comment.fromUsername}}</span>
              <span class="to-user" v-if="comment.pid"><span style="margin: 0 5px;">@</span><span class="user-name">{{comment.toUsername}}</span></span>
            </div>
            <div style="font-size: 13px;">
              <span style="color: #9c9c9c;margin-right: 20px;">{{ formatTime(comment.createTime) }}</span>
              <span @click.stop="deleteComment(comment.id)" style="cursor: pointer;color:#F56C6C">删除</span>
            </div>
          </div>
        </section-title>
      </div>
    </div>
    <div class="comment-body">
      <div class="content-text" v-html="comment.content">
      </div>
      <slot></slot>
    </div>
  </div>
</template>

<script>
import util from '@/utils/table.js'
import sectionTitle from './components/SectionTitle'

export default {
  name: "comment",
  props: {
    comment: {
      type: Object
    }
  },
  components: {
    sectionTitle,
  },
  methods: {
    formatTime(time) {
      return util.formatDate.format(new Date(time), 'yyyy-MM-dd hh:mm')
    },
    deleteComment(commentId) {
      this.$confirm('确认删除该评论?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$emit('deleteComment', commentId)
      }).catch(() => { })
    },
  }
}
</script>

<style scoped lang="less">
.comment {
  margin: 20px 0;
}

.comment-head {
  display: flex;
  .head-right {
    flex: 1;
  }
  .user-name {
    color: #8fd0cc;
  }
}

.comment-body {
  padding-left: 80px;
  .content-text {
    /*padding-bottom: 30px;*/
    margin-bottom: 30px;
    font-size: 14px;
    color: #9c9c9c;
    line-height: 1.3rem;
  }
}

.user-avatar {
  width: 50px;
  height: 50px;
  margin-right: 30px;
  img {
    width: 100%;
    height: 100%;
    border-radius: 100%;
  }
}
@media (max-width: 600px) {
  .comment-body {
    padding-left: 15px;
    .content-text {
      margin-top: 10px;
    }
  }
  .user-avatar {
    margin-right: 10px;
  }
}
</style>
